import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:seedfour/app/models/user_statistics_entity.dart';

import '../../../models/user_info_entity.dart';

class MineState {
  var personalInfo = Rxn<PersonalInfo>();

  var statisticsInfo = UserStatisticsEntity().obs;

  var selectedAccountIndex = 0.obs;
  var radioAccountIndex = 0.obs;

  late List<MineItemInfo> itemList;

  var role = ''.obs;

  MineState() {
    itemList = [
      MineItemInfo(title: '我的资产', icon: Icons.account_balance_wallet),
      MineItemInfo(title: '我的团队', icon: Icons.group),
      MineItemInfo(title: '', icon: Icons.share),
    ];
  }
}

class MineItemInfo {
  /// 栏目标题
  String title;

  /// 栏目图标
  IconData icon;

  MineItemInfo({
    required this.title,
    required this.icon,
  });
}

class PersonalInfo {
  String uid;

  String name;

  String mobile;

  String avatar;

  String cashBalanceStr;

  String marginAmountStr;

  int userRole;

  CompanyInfo currentCompany;

  List<CompanyInfo> companyList;

  PersonalInfo({
    required this.uid,
    required this.name,
    required this.mobile,
    required this.avatar,
    required this.cashBalanceStr,
    required this.marginAmountStr,
    required this.userRole,
    required this.currentCompany,
    required this.companyList,
  });

  static PersonalInfo fromUserInfoEntity(UserInfoEntity entity) {
    return PersonalInfo(
      uid: entity.uid ?? '',
      name: entity.name ?? '',
      mobile: entity.mobile ?? '',
      avatar: entity.avatar ?? '',
      cashBalanceStr: entity.cashBalanceStr ?? '',
      marginAmountStr: entity.marginAmountStr ?? '',
      userRole: entity.userRole ?? 0,
      currentCompany: CompanyInfo(
        uid: entity.targetSite?.owner ?? '',
        name: entity.targetSite?.name ?? '',
        roleType: entity.targetSite?.sysName ?? '',
        roleName: entity.targetSite?.sysCn ?? '',
      ),
      companyList: entity.privis
              ?.where((e) => e.sysName == 'clicms')
              .map((e) => CompanyInfo(
                    uid: e.owner ?? '',
                    name: e.name ?? '',
                    roleType: e.sysName ?? '',
                    roleName: e.sysCn ?? '',
                  ))
              .toList() ??
          [],
    );
  }
}

class CompanyInfo {
  String uid;

  String name;

  String roleType;

  String roleName;

  CompanyInfo({
    required this.uid,
    required this.name,
    required this.roleType,
    required this.roleName,
  });

  @override
  bool operator ==(Object other) {
    if (identical(this, other)) return true;

    return other is CompanyInfo &&
        other.uid == uid &&
        other.roleType == roleType;
  }

  @override
  int get hashCode => uid.hashCode ^ roleType.hashCode;
}
